<?php
namespace app\user\controller;

use app\common\controller\UserBase;
use think\facade\Config;
 
use app\user\model\UserGroup;
use app\user\model\UserAuthRule;
use app\user\model\User as UserModel;

class User extends UserBase
{
    protected $noAuth = ['edit_password'];
    public function index()
    {
        return $this->fetch('index');
    }
    public function index_json($limit='',$group_id='')
    {
        $user = new UserModel();
        if($group_id){
            $user = $user->where('group_id',$group_id);
        }
        $list = $user->with(['userGroup'])->order('sort_order desc,id desc')->paginate($limit);
        $this->result($list);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'user');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = UserModel::create($param);
            if ($result == true) {
                insert_user_log('添加了员工');
                $this->success('添加成功');
            } else {
                $this->error($this->errorMsg);
            }
        }    
        $user_auth = Config::load('user/auth','user_auth');
        return $this->fetch('save', ['userAuthGroup' => UserGroup::list_to_level(),'auth'=>$user_auth]);
    }

    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (empty($param['password'])) {
                unset($param['password']);
            }
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'user');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //更新数据
            $resule = UserModel::update($param,['id'=>$param['id']]);
            if ( $resule == true) {
                insert_user_log('修改了员工信息');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        $data = UserModel::where('id', $id)->find();
        $user_auth = Config::load('user/auth','user_auth');
        return $this->fetch('save', ['data' => $data,'userAuthGroup' => UserGroup::list_to_level(),'auth'=>$user_auth]);
    }
    
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            UserModel::destroy($param['id']);
            insert_user_log('删除了用户组');
            $this->success('删除成功');
        }
    }
    
    public function edit_password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            // 验证条件
            empty($param['password']) && $this->error('请输入旧密码');
            empty($param['new_password']) && $this->error('请输入新密码');
            empty($param['rep_password']) && $this->error('请输入确认密码');
            !check_password($param['new_password'], 6, 16) && $this->error('请输入6-16位的密码');
            $param['new_password'] != $param['rep_password'] && $this->error('两次密码不一致');
            $admin = UserModel::where('id', UserId())->find();
            $admin['password'] != md5($param['password']) && $this->error('旧密码错误');
            $data = ['password' => $param['new_password']];
            $result = UserModel::update($data,['id'=> UserId()]);
            if ($result == true) {
                insert_user_log('修改了登录密码');
                session('user_auth', null);
                session('user_auth_sign', null);
                $this->success('更新成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('edit_password');
    }
}
